package com.ruoyi.stuinfo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.grade.domain.Grade;
import com.ruoyi.grade.domain.Gradetemplate;
import com.ruoyi.stuinfo.domain.AcademicInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.stuinfo.domain.StuRewards;
import com.ruoyi.stuinfo.service.IStuRewardsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 获奖查询Controller
 *
 * @author zc
 * @date 2025-05-18
 */
@RestController
@RequestMapping("/stuinfo/sturewards")
public class StuRewardsController extends BaseController
{
    @Autowired
    private IStuRewardsService stuRewardsService;

    /**
     * 查询获奖查询列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:sturewards:list')")
    @GetMapping("/list")
    public TableDataInfo list(StuRewards stuRewards)
    {
        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");
        if("admin".equals(teacherId))
        {
            //管理员
            startPage();
            List<StuRewards> list = stuRewardsService.selectStuRewardsList(stuRewards);
            return getDataTable(list);
        }
        else if(hasH)
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            startPage();
            List<StuRewards> list = stuRewardsService.selectStuRewardsList_handteacher(teacherId);
            return getDataTable(list);
        }
        else
        {
            //系主任
            teacherId=teacherId.replace("X", "");
            startPage();
            List<StuRewards> list = stuRewardsService.selectStuRewardsList_Xiteacher(teacherId);
            return getDataTable(list);
        }
    }

    /**
     * 导出获奖查询列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:sturewards:export')")
    @Log(title = "获奖查询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StuRewards stuRewards)
    {

        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");
        if("admin".equals(teacherId))
        {
            //管理员
            List<StuRewards> list = stuRewardsService.selectStuRewardsList(stuRewards);
            ExcelUtil<StuRewards> util = new ExcelUtil<StuRewards>(StuRewards.class);
            util.exportExcel(response, list, "获奖查询数据");
        }
        else if(hasH)
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            List<StuRewards> list = stuRewardsService.selectStuRewardsList_handteacher(teacherId);
            ExcelUtil<StuRewards> util = new ExcelUtil<StuRewards>(StuRewards.class);
            util.exportExcel(response, list, "获奖查询数据");
        }
        else
        {
            //系主任
            teacherId=teacherId.replace("X", "");
            List<StuRewards> list = stuRewardsService.selectStuRewardsList_Xiteacher(teacherId);
            ExcelUtil<StuRewards> util = new ExcelUtil<StuRewards>(StuRewards.class);
            util.exportExcel(response, list, "获奖查询数据");
        }
    }
    /**
     * 获取获奖查询详细信息
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:sturewards:query')")
    @GetMapping(value = "/{rewardId}")
    public AjaxResult getInfo(@PathVariable("rewardId") Long rewardId)
    {
        return success(stuRewardsService.selectStuRewardsByRewardId(rewardId));
    }

    /**
     * 新增获奖查询
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:sturewards:add')")
    @Log(title = "获奖查询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StuRewards stuRewards)
    {
        return toAjax(stuRewardsService.insertStuRewards(stuRewards));
    }

    /**
     * 修改获奖查询
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:sturewards:edit')")
    @Log(title = "获奖查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StuRewards stuRewards)
    {
        return toAjax(stuRewardsService.updateStuRewards(stuRewards));
    }

    /**
     * 删除获奖查询
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:sturewards:remove')")
    @Log(title = "获奖查询", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rewardIds}")
    public AjaxResult remove(@PathVariable Long[] rewardIds)
    {
        return toAjax(stuRewardsService.deleteStuRewardsByRewardIds(rewardIds));
    }

    //模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {

        ExcelUtil<StuRewards> util = new ExcelUtil<StuRewards>(StuRewards.class);
        util.importTemplateExcel(response, "用户数据");
    }

    @PreAuthorize("@ss.hasPermi('grade:grade:adds')")
    @Log(title = "成绩详情", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult excelImport(MultipartFile file) throws Exception {
        ExcelUtil<StuRewards> util = new ExcelUtil<StuRewards>(StuRewards.class);
        List<StuRewards> stuRewards = util.importEasyExcel(file.getInputStream());
        return toAjax(stuRewardsService.insertStuRewardss(stuRewards));
    }
}
